 <?php
/* phpYahoo Class     1.0.0
 * Written by Jay (jay@gotfoo.org)
 * Project Page: http://labs.gotfoo.org/phpyahoo.php
 * Released under GNU Lesser General Public License (http://www.gnu.org/copyleft/lgpl.html)
 * For more information about the class and upcoming tools and toys using it,
 * visit http://labs.gotfoo.org/phpyahoo.php
 *
 *     
 * 
 */
class phpYahoo {
    
    var $appid = 'YAHOODEVELOPERID'; //----get one here http://api.search.yahoo.com/webservices/register_application 
    var $base_url = 'http://search.yahooapis.com/';
    var $command_web_search = 'WebSearchService/V1/webSearch';
    var $command_video_search = 'VideoSearchService/V1/videoSearch';
    var $command_related_search = 'WebSearchService/V1/relatedSuggestion';
    var $command_image_search = 'ImageSearchService/V1/imageSearch';
    
            
    function request($command,$params) 
    {                     
        $url  = $this->base_url.$command.'?'.$params.'&appid='.$this->appid;
           //echo $url;
        $c = curl_init($url);
        curl_setopt($c,CURLOPT_RETURNTRANSFER,true);
        $response = curl_exec($c);
        curl_close($c);
            
        $xml = simplexml_load_string($response);         
        
        //print_r($response);
        return $xml;
    }
    
    function request_web_search ($params)     {   
        $xml = $this->request($this->command_web_search,$params);    
           
        $results = array();
        $i = 0;
        foreach($xml->Result as $result){            
            $results[$i] = array("title"=>$result->Title,"summary"=>$result->Summary,"url"=>$result->Url,"clickUrl"=>$result->ClickUrl,"displayUrl"=>$result->DisplayUrl,"modificationDate"=>$result->ModificationDate,"mimeType"=>$result->MimeType);
            $i++;                
        }
        
        return $results;
    }
    
      function request_related_search ($params)     {   
        $xml = $this->request($this->command_related_search,$params);        
        $results = array();
        $i = 0;
        foreach($xml as $result){            
            $results[$i] = array("result"=>$result->Result);
            $i++;                
        }
        return $results;
    }
    
    function request_video_search ($params)     {   
        $xml = $this->request($this->command_video_search,$params);        
        $results = array();
        $i = 0;
        foreach($xml->Result as $result){            
            $results[$i] = array("title"=>$result->Title,"summary"=>$result->Summary,"url"=>$result->Url,"clickUrl"=>$result->ClickUrl,"refererUrl"=>$result->RefererUrl,"fileSize"=>$result->FileSize,"fileFormat"=>$result->FileFormat,"weight"=>$result->Height,"width"=>$result->Width,"duration"=>$result->Duration,"streaming"=>$result->Streaming,"thumbnail"=>$result->Thumbnail->Url,"theight"=>$result->Thumbnail->Height,"twidth"=>$result->Thumbnail->Width);
            $i++;                
        }    
        
        return $results;
    } 
    
    function request_image_search ($params)     {   
        $xml = $this->request($this->command_image_search,$params);        
        $results = array();
        $i = 0;
        foreach($xml->Result as $result){            
            $results[$i] = array("title"=>$result->Title,"summary"=>$result->Summary,"url"=>$result->Url,"clickUrl"=>$result->ClickUrl,"refererUrl"=>$result->RefererUrl,"fileSize"=>$result->FileSize,"fileFormat"=>$result->FileFormat,"weight"=>$result->Height,"width"=>$result->Width,"thumbnail"=>$result->Thumbnail->Url,"theight"=>$result->Thumbnail->Height,"twidth"=>$result->Thumbnail->Width);
            $i++;                
        }    
        
        return $results;
    } 


    
    function createQueryString($query,$results=null){        
        if (is_array($terms)) {
            $extras = implode(" OR ", $query);
            $extras = urlencode($extras);    
            $queryString = "query=$extras";                 
        }else{      
            $query = urlencode($query);  
            $queryString = "query=$query";
           }           
           $queryString .= "&results=$results";           
           return $queryString;
    }
    
    function searchWeb($query,$results=null){
        //http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=madonna&results=2                  
        
           $response =  $this->request_web_search($this->createQueryString($query,$results));
           return $response;
    }
    
    function searchRelated($query,$results=null){
        //http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=madonna&results=2                   
           $response =  $this->request_related_search($this->createQueryString($query,$results));
           return $response;
    }
    
    
    
    function searchVideos($query){
        //http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=madonna&results=2           
        
          $response =  $this->request_video_search($this->createQueryString($query,$results));       
           return $response;
    }
    
    function searchImages($query){
        //http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=madonna&results=2           
        
          $response =  $this->request_image_search($this->createQueryString($query,$results));       
           return $response;
    }
}

?> 